﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;

namespace Abigail.Ui.Article
{
    public partial class UiViewArticles : Form
    {
        DataTable t = new DataTable();
        BindingSource b = new BindingSource();
        Controller c;

        public UiViewArticles(Controller givenC)
        {
            InitializeComponent();
            c = givenC;
            fillData(t);
            GenerateMenu();

            foreach (Model.HeaderList header in c.ArticleDataTableHeaderList)
                articles.Columns[header.SwedishName].FillWeight = header.Weight;

            this.Show();
        }

        private void GenerateMenu()
        {
            int i = 0;
            while (i < c.ArticleDataTableHeaderList.Count)
            {
                ToolStripMenuItem show = new ToolStripMenuItem(c.ArticleDataTableHeaderList[i].SwedishName, null, null, c.ArticleDataTableHeaderList[i].Name);
                show.CheckOnClick = true;
                show.CheckedChanged += new System.EventHandler(this.ShowItem_CheckStateChanged);
                show.Checked = c.ArticleDataTableHeaderList[i].Visible;
                showMenu.DropDownItems.Add(show);

                articles.Columns[i].Visible = c.ArticleDataTableHeaderList[i].Visible;
                i++;
            }
        }

        public void fillData(DataTable t)
        {
            // Render data onto the screen
            articles.Invalidate();
            t = c.GetArticlesAsDataTable();
            b.DataSource = t;
            articles.DataSource = b;
        }

        private void articles_CellContentDoubleClick(object sender, DataGridViewCellEventArgs e)
        {
            Int32 row = (int)articles.SelectedCells[0].RowIndex; //get what row the user is clicking
            Int32 col = 0; // we always want the value at the first column since this is the ID
            Int32 val = int.Parse(articles.Rows[row].Cells[col].FormattedValue.ToString());
            c.OpenUiEditArticle(val);
        }

        private void updateraToolStripMenuItem_Click(object sender, EventArgs e)
        {
            fillData(t);
        }

        private void skapaNyToolStripMenuItem_Click(object sender, EventArgs e)
        {
            c.OpenUiNewArticle();
        }

        private void stängToolStripMenuItem_Click(object sender, EventArgs e)
        {
            this.Close();
        }

        private void ShowItem_CheckStateChanged(object sender, EventArgs e)
        {
            try
            {
                foreach (Model.HeaderList item in c.ArticleDataTableHeaderList)
                {
                    if (item.Name == ((ToolStripMenuItem)sender).Name)
                        item.Visible = ((ToolStripMenuItem)sender).Checked;
                    articles.Columns[((ToolStripMenuItem)sender).Text].Visible = ((ToolStripMenuItem)sender).Checked;
                }
            }
            catch (Exception e2)
            {
                Popup.Error(e2.ToString(), e2.GetType().ToString());
            }
        }

        private void resetDefaultView_Click(object sender, EventArgs e)
        {
            Popup.FunctionNotImplemented();
        }

        
    }
}
